<?php

namespace App\Http\Controllers\Coupon;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Coupon;
use App\Models\CouponLog;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Car\Entities\CarOrder;

class CouponLogController extends Controller
{
    use ResponseData;
    // 判断是否领取过未使用优惠券
    protected function getHistoryCounpon($coupon_id,$openid){
        return CouponLog::query()->select('id')->where('status',0)->where('openid',$openid)->where('coupon_id',$coupon_id)->first();
    }
    // 手动领取优惠券
    public function getCouponHand(CouponLog $couponLog,Request $request){
        // 判断是否老客
        $order = CarOrder::query()->where('openid',$request->openid)->first();
        // 找出ID对应的优惠券
        $couponitem = Coupon::query()->where('id',$request->id)->first();
        // 查找是否已领取但未使用
        $couponlogunused = $couponLog->query()->where('coupon_id',$request->id)->where('status',0)->first();
        if($order){
            // 老客只能领老客的
            if($couponitem->face == 1){
                return $this->failed('只能新用户领取',201);
            }
            if($couponlogunused){
                return $this->failed('您已领取此优惠券暂未使用，无法重复领取',201);
            }
            $data = [];
            $data['name'] = $couponitem->name;
            $data['coupon_id'] = $couponitem->id;
            $data['openid'] = $request->openid;
            $data['price'] = $couponitem->price;
            $data['created_at'] = now();
            $data['updated_at'] = now();
            CouponLog::query()->create($data);
            return $this->success('领取成功');
        }else{
            // 新客领取
            if($couponitem->face == 2){
                return $this->failed('只能老用户领取',201);
            }
            $couponlognew = $couponLog->query()->where('coupon_id',$request->id)->first();
            if($couponlognew){
                return $this->failed('新客优惠券只能领取一张',201);
            }
            $data = [];
            $data['name'] = $couponitem->name;
            $data['coupon_id'] = $couponitem->id;
            $data['openid'] = $request->openid;
            $data['price'] = $couponitem->price;
            $data['created_at'] = now();
            $data['updated_at'] = now();
            CouponLog::query()->create($data);
            return $this->success('领取成功');
        }
    }
    // 优惠券领取
    public function getCouponUpdate(CouponLog $couponLog,Request $request){
        // 判断是否老客
        $order = CarOrder::query()->where('openid',$request->openid)->first();

        if($order){ // 老客
            $data = [];
            $counpons = Coupon::query()->where('auto_fetch',1)->where('is_show',1)->get();
            if(count($counpons)<=0){
                return $this->failed('未找到优惠券',201);
            }
            foreach ($counpons as $k=>$item){
                if($item->face==2){
                    $isHasCounpon = $this->getHistoryCounpon($item->id,$request->openid);
                    if(!$isHasCounpon){
                        $data[$k]['name'] = $item->name;
                        $data[$k]['coupon_id'] = $item->id;
                        $data[$k]['openid'] = $request->openid;
                        $data[$k]['price'] = $item->price;
                        $data[$k]['created_at'] = now();
                        $data[$k]['updated_at'] = now();
                    }
                }
            }
            if(count($data)>0){
                DB::table('coupon_logs')->insert($data);
            }
        }else{// 新客
            $data = [];
            $counpons = Coupon::query()->where('auto_fetch',1)->where('is_show',1)->where('face',1)->first();
            if(!$counpons){
                return $this->success('未找到优惠券');
            }
            $isHasCounpon = $this->getHistoryCounpon($counpons->id,$request->openid);
            if (!$isHasCounpon) {
                $data['name'] = $counpons->name;
                $data['coupon_id'] = $counpons->id;
                $data['openid'] = $request->openid;
                $data['price'] = $counpons->price;
                $data['created_at'] = now();
                $data['updated_at'] = now();
                CouponLog::query()->create($data);
            } else {
                return $this->success('只能领取一张新客优惠券');
            }
            
        }

        return $this->success('领取成功');
    }
    //领取记录表
    public function list(CouponLog $couponLog,Request $request){
        $size = $request->input('size',10);
        $data = $couponLog->query()->where('openid',$request->openid)->orderBy('id','desc')->paginate($size);
        return $this->success($data);
    }
    //领取优惠券
    public function get(CouponLog $couponLog,Request $request){
        $isUse = $couponLog->where('openid',$request->openid)->first();
        if($isUse){
            return $this->failed('已经领取过,请勿重复领取');
        }
        // $data = $request->all();
        // $coupon = $this->get_coupon($request->coupon_id);
        // if($coupon){
        //     $data['name']= $coupon->name;
        //     $user = $this->getUser($request->openid);
        //     $data['nick_name'] = $user->nickName;
        //     $data['price'] = $coupon->price;
        //     $couponLog->create($data);
        // }

        return $this->success('领取成功');
    }
    public function get_coupon($id){
        return Coupon::query()->find($id);
    }

    protected function getUser($openid){
        return User::where('openid',$openid)->first();
    }
}
